#include<bits/stdc++.h>
using namespace std;

long long readd(){
    char c;
    long long x=0;
    int f=1;
    c=getchar();
    while(c<'0'||c>'9'){
        if(c=='-') f=-1;
        c=getchar();
    }
    while('0'<=c&&c<='9'){
        x=x*10+(c-'0');
        c=getchar();
    }
    return x * (long long)f;
}

struct edge{
    int to,v,next;
    edge(){};
    edge(int to_,int v_,int next_){
        to=to_,v=v_,next=next_;
    }
}edges[800010];

map<int,int> m;
int n,p,arr[200010],head[200010];
bool vis[200010];

void add_edge(int from,int to,int v){
    edges[p]=edge(to,v,head[from]);
    head[from]=p;
    p++;
}

long long dfs(int now,long long ans){
    if(now==n-1)return ans;
    if(vis[now])return 0;
    vis[now]=true;
    long long sum=0;
    for(int i=head[now];i!=-1;i=edges[i].next){
        if(!vis[edges[i].to])sum=max(sum,dfs(edges[i].to,ans+edges[i].v));
    }
    vis[now]=false;
    return sum;
}

int main()
{
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    int t;
    t=readd();
    while(t--){
        p=0;
        n=readd();
        for(int i=0;i<=20000;i++) m[i]=-1;
        for(int i=0;i<n;i++)head[i]=-1,vis[i]=false;
        for(int i=0;i<n;i++){
            arr[i]=readd();
            if(m[arr[i]]!=-1){
                add_edge(m[arr[i]],i,arr[i]);
                add_edge(i,i-1,0);
            }
            else if(i!=0) add_edge(i-1,i,0);
            m[arr[i]]=i;
        }

        cout<<dfs(0,0)<<'\n';
    }

    return 0;
}
